#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<cstring>
#include<arpa/inet.h>
#include "stdShared.h"


#include <json-c/json.h>


using namespace std;
#define PORT 8888
#define Size 64

struct Msg
{
    int m_flag; 
    char m_name[Size];
    char m_passwd[Size];

    
};

void menu()
{
    //system("clear");
    cout << "1.注册" <<endl;
    cout << "2.登录" << endl;
    cout << "3.退出" << endl;

}

void registerFunc(int sockfd)
{
    char username[Size];
    cout<< "请输入用户名:";
    scanf("%s",username);

    char passwd[Size];
    cout<< "请输入密码:";
    scanf("%s",passwd);

    /* 创建一个json对象 */
    json_object * jsonObj = json_object_new_object();
    json_object_object_add(jsonObj,"type",json_object_new_int(REGISTER));
    json_object_object_add(jsonObj,"username",json_object_new_string(username));
    json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));
    
    /* 将json对象转换成字符串 */
    const char * jsonStr = json_object_to_json_string(jsonObj);

    

    /* 发送信息 */
    int length = strlen(jsonStr);
    cout << "length : " << length << endl;
    write(sockfd,&length,sizeof(int));
    write(sockfd,jsonStr,strlen(jsonStr));
}

void loginFunc(int sockfd)
{
    char username[Size];
    cout<< "请输入用户名:";
    scanf("%s",username);

    char passwd[Size];
    cout<< "请输入密码:";
    scanf("%s",passwd);

  /* 创建一个json对象 */
    json_object * jsonObj = json_object_new_object();
    json_object_object_add(jsonObj,"type",json_object_new_int(LOGIN));
    json_object_object_add(jsonObj,"username",json_object_new_string(username));
    json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));
    
    /* 将json对象转换成字符串 */
    const char * jsonStr = json_object_to_json_string(jsonObj);


    /* 发送信息 */
    int length = strlen(jsonStr);
    cout << "length : " << length << endl;
    write(sockfd,&length,sizeof(int));
    //usleep(500);
    write(sockfd,jsonStr,strlen(jsonStr));

}

int main()
{
    //创建通信文件描述符
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        perror("socket create error:");
        _exit(-1);
    }

    //连接服务器
    sockaddr_in serverAddress;
    memset(&serverAddress,0,sizeof(serverAddress));
    //地址族
    serverAddress.sin_family = AF_INET; //ipv4
    //端口
    serverAddress.sin_port = htons(PORT);
    //服务器IP
    const char * serverIp = "192.168.248.128";
    inet_pton(AF_INET,serverIp,&(serverAddress.sin_addr.s_addr));
    //连接服务器
    connect(sockfd,(const sockaddr *)&serverAddress,sizeof(serverAddress));
    
    
    char buffer[Size] = {0};
    while(1)
    {
        memset(buffer,0,sizeof(buffer));
        menu();
        int choice = 0;
        cout << "请输入:";
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                registerFunc(sockfd);
                break;
            case 2:
                loginFunc(sockfd);
                break;
            case 3:
                break;
            default:
                break;


        }
        //客户端发送hello world 
        // cout << "请输入:" ;
        // fgets(buffer,sizeof(buffer),stdin);
        // write(sockfd,(const void *)buffer,strlen(buffer));
        
        // memset(buffer,0,sizeof(buffer));
        // read(sockfd,(void *)buffer,Size);
        // cout << buffer ;
        //sleep(8);
    }

    close(sockfd);
    return 0;
}